using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ns20;

namespace ns19
{
	internal class Class89
	{
		private readonly string string_0;

		private int int_0;

		[CompilerGenerated]
		private List<object> list_0;

		public List<object> List_0
		{
			[CompilerGenerated]
			get
			{
				return list_0;
			}
			[CompilerGenerated]
			private set
			{
				list_0 = value;
			}
		}

		public Class89(string expression)
		{
			Class204.smethod_2(expression, "expression");
			string_0 = expression;
			List_0 = new List<object>();
			method_0();
		}

		private void method_0()
		{
			int num = int_0;
			bool flag = false;
			while (int_0 < string_0.Length)
			{
				char c = string_0[int_0];
				switch (c)
				{
				default:
					if (flag)
					{
						throw new JsonException("Unexpected character following indexer: " + c);
					}
					break;
				case '.':
					if (int_0 > num)
					{
						string item2 = string_0.Substring(num, int_0 - num);
						List_0.Add(item2);
					}
					num = int_0 + 1;
					flag = false;
					break;
				case '(':
				case '[':
					if (int_0 > num)
					{
						string item = string_0.Substring(num, int_0 - num);
						List_0.Add(item);
					}
					method_1(c);
					num = int_0 + 1;
					flag = true;
					break;
				case ')':
				case ']':
					throw new JsonException("Unexpected character while parsing path: " + c);
				}
				int_0++;
			}
			if (int_0 > num)
			{
				string item3 = string_0.Substring(num, int_0 - num);
				List_0.Add(item3);
			}
		}

		private void method_1(char char_0)
		{
			int_0++;
			char c = ((char_0 == '[') ? ']' : ')');
			int startIndex = int_0;
			int num = 0;
			bool flag = false;
			char c2;
			while (true)
			{
				if (int_0 < string_0.Length)
				{
					c2 = string_0[int_0];
					if (char.IsDigit(c2))
					{
						num++;
						int_0++;
						continue;
					}
					if (c2 != c)
					{
						break;
					}
					flag = true;
				}
				if (!flag)
				{
					throw new JsonException("Path ended with open indexer. Expected " + c);
				}
				if (num == 0)
				{
					throw new JsonException("Empty path indexer.");
				}
				string value = string_0.Substring(startIndex, num);
				List_0.Add(Convert.ToInt32(value, CultureInfo.InvariantCulture));
				return;
			}
			throw new JsonException("Unexpected character while parsing path indexer: " + c2);
		}

		internal JToken method_2(JToken jtoken_0, bool bool_0)
		{
			JToken jToken = jtoken_0;
			foreach (object item in List_0)
			{
				string text = item as string;
				if (text != null)
				{
					JObject jObject = jToken as JObject;
					if (jObject != null)
					{
						jToken = jObject[text];
						if (jToken == null && bool_0)
						{
							throw new JsonException("Property '{0}' does not exist on JObject.".smethod_0(CultureInfo.InvariantCulture, text));
						}
						continue;
					}
					if (bool_0)
					{
						throw new JsonException("Property '{0}' not valid on {1}.".smethod_1(CultureInfo.InvariantCulture, text, jToken.GetType().Name));
					}
					return null;
				}
				int num = (int)item;
				JArray jArray = jToken as JArray;
				JConstructor jConstructor = jToken as JConstructor;
				if (jArray != null)
				{
					if (jArray.Count > num)
					{
						jToken = jArray[num];
						continue;
					}
					if (bool_0)
					{
						throw new JsonException("Index {0} outside the bounds of JArray.".smethod_0(CultureInfo.InvariantCulture, num));
					}
					return null;
				}
				if (jConstructor != null)
				{
					if (jConstructor.Count > num)
					{
						jToken = jConstructor[num];
						continue;
					}
					if (bool_0)
					{
						throw new JsonException("Index {0} outside the bounds of JConstructor.".smethod_0(CultureInfo.InvariantCulture, num));
					}
					return null;
				}
				if (bool_0)
				{
					throw new JsonException("Index {0} not valid on {1}.".smethod_1(CultureInfo.InvariantCulture, num, jToken.GetType().Name));
				}
				return null;
			}
			return jToken;
		}
	}
}
